import java.util.HashSet;

/**
 * @author 35326
 * 2021/3/20 21:10
 *
 * Set的课后练习题，练习对Set的理解
 */
public class SetTest {
	public static void main(String[] args) {
		HashSet<Person> set = new HashSet<>();
		Person person1=new Person(1,"AA");
		Person person2=new Person(2,"CC");

		//为set添加两个元素
		set.add(person1);
		set.add(person2);
		System.out.println(set);

		//修改person1的值 然后remove
		person1.name="CC";
		set.remove(person1);
		System.out.println("修改后set的值为："+set);


		//最后set的值为：
		Person person3=new Person(1,"AA");
		set.add(person3);
		System.out.println("最后set的值为："+set);

	}
}


class Person{
	int number;
	String name;

	public Person(int number, String name) {
		this.number = number;
		this.name = name;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (o == null || getClass() != o.getClass()) {
			return false;
		}

		Person person = (Person) o;

		if (number != person.number) {
			return false;
		}
		return name != null ? name.equals(person.name) : person.name == null;
	}

	@Override
	public int hashCode() {
		int result = number;
		result = 31 * result + (name != null ? name.hashCode() : 0);
		return result;
	}

//	@Override
//	public String toString() {
//		return "Person{" +
//				"number=" + number +
//				", name='" + name + '\'' +
//				'}';
//	}
}